package com.atm.bulksms.dialog;

import java.text.DateFormat;
import java.util.Date;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.atm.bulksms.BulkSMSActivity;
import com.atm.bulksms.R;
import com.atm.bulksms.StatusActivity;
import com.atm.bulksms.R.id;
import com.atm.bulksms.R.layout;
import com.atm.bulksms.data.BulkSMSData.SMSData;
public class MessageDetailsDialog extends Dialog{

	LayoutInflater inflater ;
	//StatusActivity context;
	public MessageDetailsDialog(StatusActivity context, Cursor cursor) {
		super(context);
	//	this.context = context;
		inflater = LayoutInflater.from(context);
		setTitle("Message Details");
		
		setContentView(R.layout.message_details_dialog);
		//updateMessage(cursor);
		
		Button btn = (Button)findViewById(R.id.msg_details_ok);
		btn.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
					cancel();				
			}});
	}

	public void updateMessage(Cursor cursor) {
		/*context.runOnUiThread(new Runnable() {
		    public void run() {
		       showProgressBar();
		    }
		} );*/
		TableLayout table = (TableLayout) findViewById(R.id.number_table);
		System.out.println(" now count "+cursor.getCount());
		table.removeAllViews();
		while(cursor.moveToNext()){
			TableRow row = (TableRow)inflater.inflate(R.layout.message_details_item, null);
			String number = cursor.getString(cursor.getColumnIndex(SMSData.RECEPIENT));
			DateFormat mFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
			DateFormat tFormat = DateFormat.getTimeInstance();
			Date dt = new Date(cursor.getLong(cursor.getColumnIndex(SMSData.SENT_TIME)));
			String time = mFormat.format(dt)+"\n"+tFormat.format(dt);
			String status = getStatus(cursor.getInt(cursor.getColumnIndex(SMSData.STATUS)));
			((TextView)row.findViewById(R.id.item_number)).setText(getName(number));
			((TextView)row.findViewById(R.id.item_time)).setText(time);
			((TextView)row.findViewById(R.id.item_status)).setText(status);
			table.addView(row);
		}
		System.out.println("table rows :"+table.getChildCount());
		cursor.moveToFirst();
		((TextView)findViewById(R.id.message_text)).setText(cursor.getString(cursor.getColumnIndex(SMSData.MESSAGE)));
		//cursor.close();
		
//		       progress.cancel();
	}

	/*ProgressDialog progress;
	protected void showProgressBar() {
		if(progress == null){
			progress = new ProgressDialog(context);
			progress.setMessage("Loading ..\n Please wait.");
		}
		progress.show();
	}*/

	private String getStatus(int id) {
		String status = "N.A";
		switch (id) {
		case BulkSMSActivity.STATUS_SENDING:
			status = "Sending";
			break;
		case BulkSMSActivity.STATUS_SENT:
			status = "Sent";
			break;
		case BulkSMSActivity.STATUS_RECEIVED:
			status = "Received";
			break;
		default:
			break;
		}
		return status;
	}

	private String getName(String phoneNumber) {
		Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
		Cursor c =  getContext().getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
		if(c != null && c.getCount() > 0){
			c.moveToFirst();
		String name = c.getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
		System.out.println(" name : "+name);
		c.close();
		return name+"\n"+phoneNumber;
		}
		return phoneNumber;
	}
	
	
	
	

}
